package org.uugu.counter.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.uugu.counter.R;
import org.uugu.counter.Support.CommonParam;
import org.uugu.counter.Support.DatabaseUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * Created by Silence on 2014/3/28.
 */
public class AddGroupActivity extends Activity {

    private Button button;

    private DatabaseUtils dbUtils;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addgroup);

        dbUtils = new DatabaseUtils(this, CommonParam.DB_NAME, CommonParam.VERSION);

        button = (Button)findViewById(R.id.addGroupBtn);
        button.setOnClickListener(new ButtonListener());
    }


    class ButtonListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // 获取groupName
            EditText groupName = (EditText)findViewById(R.id.groupText);
            Bundle data = new Bundle();
            String groupNm = groupName.getText().toString();

            DateFormat df = new SimpleDateFormat("MM-dd hh:mm:ss");
            String date = df.format(new Date());
            // 插入DB
            dbUtils.getWritableDatabase().execSQL("INSERT INTO groups values(null, ?, ?)", new String[]{date, groupNm});
            Cursor cusor = dbUtils.getReadableDatabase().query("groups", new String[]{"id"}, "date = ?", new String[]{date}, null, null, "id asc");
            int id = 0;
            while(cusor.moveToNext()) {
                id = cusor.getInt(0);
            }
            data.putInt("id", id);
            data.putString("groupName", groupNm);
            Intent intent = new Intent(AddGroupActivity.this, WorkActivity.class);
            intent.putExtras(data);

            startActivity(intent);
            finish();
        }

    }

}